$( document ).ready(function() {
	var $filterCB = $('.filterCB');
	var $mergeCB = $('.mergeCB');
	var $archiveCB = $('.archiveCB');
	var $tableHeader = $('.pclistHeader');
	setCBFilters();
	
	$filterCB.on('change',function(event){
		setCBFilters();
	});
	
	$mergeCB.on('change',function(event){
		merge();
	});
	
	$archiveCB.on('change', function(event){
		archive(this);
	});
	
	$tableHeader.on('click',function(event){
		var sT = 6;	
		var $class = $(this).find('span').attr('class');
		var orderBy = 'DESC';
		if($class != 'glyphicon glyphicon-chevron-down'){
			var orderBy = 'ASC';
		}
		switch($(this).attr('id'))
		{
		case 'pcn': sT=3;
		break;
		case 'pct': sT=1;
		break;
		case 'pip': sT=2;
		break;
		case 'pcpun': sT=5;
		break;
		case 'posn': sT=4;
		break;
		default: sT=6;
		break;
		}
		location.href='/computerlist.php?sortType='+sT+'&orderBy='+orderBy;
	});
	
	function merge(){
		if($mergeCB.filter(':checked').length == 2){
			var i = 0;
			$mergeCB.filter(':checked').each(function(){
				if(i == 0){
					pcid1= $(this).attr("value");
				}else{
					pcid2= $(this).attr("value");
				}
				i++;
			});
			if(confirm('Die beiden Computer verbinden? Diese Aktion kann nicht rückgänig gemacht werden!')){
				//alert("ID1: "+pcid1+"ID2: "+pcid2);
				window.location.href = 'supportSites/pc_merge.php?PC_ID1=' + pcid1 + "&PC_ID2=" + pcid2;
			}else{
				$mergeCB.attr("checked",false);
			}
		}
	}
	
	function archive(element){
		window.location.href = 'supportSites/pc_archiv.php?PC_ID=' + element.value + "&archiv=" + element.checked;
	}
	
	function setCBFilters (){
		countChecked = $filterCB.filter(':checked').length;
		if(countChecked >= 3 ){
			//alert("hopp");
			$filterCB.filter(':not(:checked)').each(function(){
				$(this).attr("disabled",true);
			});
		}else{
			$filterCB.filter(':not(:checked)').each(function(){
				$(this).attr("disabled",false);
			});
		}
		//console.log($('#ram_size').prop('checked'));
//		$filterCB.each(function(){
//			console.log(this);
//			if($(this).prop('checked')){
//				countChecked = countChecked +1 ;
//			}
//		});
//		alert('Anzahl Checked: ' + countChecked);
	}
	/*
	$("#ram_size").attr("disabled", true);
	console.log($('#ram_size'));
	*/
});